<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

/**
 * SESSION类，使用方法：
 * $sess1=new cls_file_session('folder1','namespace1');
 * $sess2=new cls_file_session('folder1','namespace2');
 * $sess1->sess_set('var1','a');
 * $sess1->sess_set('var2','b');
 * $sess2->sess_set('var1','c');
 * $sess2->sess_set('var2','d');
 * 
	$sess1：
	sess_path:D:/Essamp/PHP5/sessiondata/folder1
	sess_file:sess_ffc9e2dfa36b92aa1db7df978173b021
	sess_name:PHPSESSID
	sess_root_path:D:/Essamp/PHP5/sessiondata/
	sess_folder:folder1
	sess_namespace:namespace1
	sess_prefix:sess_
	sess_id:ffc9e2dfa36b92aa1db7df978173b021
	
	$sess2：
	sess_path:D:/Essamp/PHP5/sessiondata/folder2
	sess_file:sess_ffc9e2dfa36b92aa1db7df978173b021
	sess_name:PHPSESSID
	sess_root_path:D:/Essamp/PHP5/sessiondata/
	sess_folder:folder2
	sess_namespace:namespace2
	sess_prefix:sess_
	sess_id:ffc9e2dfa36b92aa1db7df978173b021	
	
	Array
	(
	    [sess1] => Array
	        (
	            [var1] => a
	            [var2] => b
	        )
	
	    [sess2] => Array
	        (
	            [var1] => c
	            [var2] => d
	        )
	
	)
 * 
 */

require_once(dirname(__FILE__) . '/cls_variable.php');

class cls_file_session extends cls_variable{
	
	private static $sess_path='';
	private static $sess_file='';
	private static $sess_name='';
	private static $sess_root_path='';

	private $sess_folder='default';
	private $sess_namespace='default';
	private $sess_prefix='sess_';
	
	private static $sess_id='';
	private $expiry=200; 
	
	private static $sess=NULL;
	
	public static function start()
	{
		session_start();
	}
	
	public function cls_file_session($sess_folder='',$sess_namespace='')
	{
		session_set_save_handler( 
		array (& $this, "open_session"), 
		array (& $this, "close_session"), 
		array (& $this, "read_session"), 
		array (& $this, "write_session"), 
		array (& $this, "destroy_session"), 
		array (& $this, "GC"));
		
		$this->sess_folder=!empty($sess_folder)? trim($sess_folder) : 'default';
		$this->sess_namespace=!empty($sess_namespace)? trim($sess_namespace) : 'default';
		
		session_start();
	}
	
	//public function __construct($sess_folder='',$sess_namespace='')
	//{
	//	$this->cls_file_session($sess_folder,$sess_namespace);
	//}
	
	public function __destruct()
	{
		$this->destroy_expired_session();
	}
	
	public static function init($sess_folder='',$sess_namespace='')
	{
		if(NULL==self::$sess)
		{
			self::$sess=new cls_file_session($sess_folder,$sess_namespace);
		}
		return self::$sess;
	}
	
	public function open_session($sess_root_path, $session_name)
	{
		self::$sess_root_path=$this->transact_path($sess_root_path);
		self::$sess_path=$this->transact_path($sess_root_path).$this->sess_folder;
		
		if(!file_exists(self::$sess_path))//如果文件夹不存在，那么建文件夹
		{
			$this->create_dir(self::$sess_path);
		}
		
		self::$sess_name=$session_name;
		return true;
	}
	
	public function close_session()
	{
		return true;
	}	

	public function read_session($id)
	{
		self::$sess_id=$id;

		self::$sess_file=$this->sess_prefix.$id;

		return $this->read_file(self::$sess_path,self::$sess_file);
	}
	
	public function write_session($id, $sess_data)
	{
		self::$sess_id=$id;
	
		self::$sess_file=$this->sess_prefix.$id;
		
		return $this->write_file(self::$sess_path,self::$sess_file, $sess_data,'rewrite');
	}
	
	public function destroy_session()
	{
		@unlink(self::$sess_path.'/'.self::$sess_file);
	}
	
	public function GC($maxlifetime)
	{
		return true;
	}
	
	/*****************************************************************/
	
	/*
	 * 删除folder1文件夹里所有过期的session
	 */
	private function destroy_expired_session()
	{
		$current_time=time();
		
		$sess_files=$this->get_files(self::$sess_path);
		
		if(!empty($sess_files))
		{
			foreach($sess_files as $key=>$file)
			{
				if($current_time-$this->file_a_time($file,'')>=$this->expiry)
				{
					@unlink($file);
				}				
			}
		}
	}
	
	/*
	 * folder1文件夹里有几个session活跃
	 */
	public function get_sess_number()
	{
		$sess_files=$this->get_files(self::$sess_path);
		return count($sess_files);
	}
	
	private function gen_sess_id()
	{	
		$id=$this->random_string('alnum', 32);
		while(file_exists(self::$sess_path.'/'.$this->sess_prefix.$id))
		{
			$id=$this->random_string('alnum', 32);
		}
		return $id;
	}
	
	public function regenerate_id()
	{
		//sess_path:D:/Essamp/PHP5/sessiondata/folder1
		//sess_file:sess_ffc9e2dfa36b92aa1db7df978173b021
		//sess_name:PHPSESSID
		//sess_root_path:D:/Essamp/PHP5/sessiondata/
		//sess_folder:folder1
		//sess_namespace:namespace1
		//sess_prefix:sess_
		//sess_id:ffc9e2dfa36b92aa1db7df978173b021
		
		$new_id=$this->gen_sess_id();
		
		//读取旧文件的内容并写到新文件里去
		$sess_content=$this->read_file(self::$sess_path,self::$sess_file);
		$this->write_file(self::$sess_path,$this->sess_prefix.$new_id,$sess_content,$op='rewrite');
		
		//更新成员变量
		self::$sess_file=$this->sess_prefix.$new_id;
		self::$sess_id=$new_id;
		@setcookie(self::$sess_name, $new_id , time()+ $this->expiry);//3600秒=1小时  	
	}
	
	/*****************************************************************/
	public function get_sess_id()
	{
		return self::$sess_id;
	}
	
	public function get_sess_path()
	{
		return self::$sess_path;
	}
	
	public function get_sess_root_path()
	{
		return self::$sess_root_path;
	}
	
	public function get_sess_file()
	{
		return self::$sess_file;
	}
	
	public function get_sess_name()
	{
		return self::$sess_name;
	}
		
	public function get_sess_folder()
	{
		return $this->sess_folder;
	}
	
	public function get_sess_namespace()
	{
		return $this->sess_namespace;
	}
	
	public function get_sess_prefix()
	{
		return $this->sess_prefix;
	}	
	/*****************************************************************/
	
	public function value($key='')
	{
		if($key==='')
		{
			return $GLOBALS['_SESSION'][$this->sess_namespace];
		}
		else{
			return $GLOBALS['_SESSION'][$this->sess_namespace][$key];
		}
	}
	
	public function un_set($key)
	{
		if($key==='')
		{
			return false;
		}
		else{
			unset($GLOBALS['_SESSION'][$this->sess_namespace][$key]);
		}	
	}
	
	public function set($key='',$value)
	{
		if($key==='')
		{
			return false;
		}
		else{
			$GLOBALS['_SESSION'][$this->sess_namespace][$key]=$value;
		}
	}
	
	/*
	 * 删除namespace里的session
	 */
	public function destroy()
	{
		unset($GLOBALS['_SESSION'][$this->sess_namespace]);
	}
	
	/*
	 * namespace里的某个session是否存在
	 */
	public function is_set($key)
	{
		if(isset($GLOBALS['_SESSION'][$this->sess_namespace][$key]))
		{
			return true;
		}
		else{
			return false;
		}
	}
	

}

?>